<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
	<!-- 引入整个common页面 -->
	<div th:replace="common/common :: head">
	</div>
</head>
<body>
	<div class="content_wrap">
		<div class="zTreeDemoBackground left">
			<ul id="treeDemo" class="ztree"></ul>
		</div>
		
		<script type="text/javascript">
		//创建配置
		var setting = {
				async: {
					enable: true,
					url: "/menuController/getzTree",
					autoParam: ["id", "name"]
				},
				data: {
					simpleData: {
						enable: true
					}
				},check: {
					enable: true,
					chkStyle: "checkbox",
					chkboxType: { "Y": "p", "N": "s" }
				},view : {
			    	addHoverDom: function(treeId, treeNode){  
						var aObj = $("#" + treeNode.tId + "_a"); // tId = permissionTree_1, ==> $("#permissionTree_1_a")
						// aObj.attr("href", "javascript:;");
						if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
						var s = '<span id="btnGroup'+treeNode.tId+'">';
						if ( treeNode.level == 0 ) {
							s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
						} else if ( treeNode.level == 1 ) {
							s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
							if (treeNode.children == undefined) {
								s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#" >删除</a>';
							}
							s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
						} else if ( treeNode.level == 2 ) {
							s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
							s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#">删除</a>';
						}		
						s += '</span>';
						aObj.after(s);
					},
					removeHoverDom: function(treeId, treeNode){
						$("#btnGroup"+treeNode.tId).remove();
					}
			    }
			};
		
		
			
		//初始化树
		$(document).ready(function(){
			$.fn.zTree.init($("#treeDemo"), setting);
		});
		 		
		function addNode(parentid){
			BootstrapDialog.show({
	            title: '增加',
	            message: $('<div></div>').load('/menuController/addInfo'),
	            /* type: BootstrapDialog.TYPE_SUCCESS, */
	            size: BootstrapDialog.SIZE_SMALL, 
	            buttons: [{
	                label: '取消',
	                cssClass : "btn btn-info",
	                action: function (dialog) {
	                	dialog.close();
	                }
	            }, {
	                label: '确定',
	                cssClass : "btn btn-info",
	                action: function (dialog) {
	                	alert(button);
						alert(perms);
	    				$.ajax({
	    					url:"/menuController/addInfos",
	    					data:{text:$("#text").val(),url:$("#url").val(),button:$("#button").val(),perms:$("#perms").val(),parentid:parentid},
	    					dataType:"json",
	    					type:"post",
	    					async:true,
	    					success:function(result){
	    						if(result){
	        						dialog.close();
	        						toastr.success("增加成功");
	        						$.fn.zTree.init($("#treeDemo"), setting);
	    						}else{
	    							toastr.error("增加失败")
	    						}

	    					},
	    					error:function(){
	    						toastr.error("系统异常,请联系管理员");
	    					}
	    				})
	                }
	            }]
	        });
		}
		
		function editNode(id){
			BootstrapDialog.show({
	            title: '修改',
	            message: $('<div></div>').load('/menuController/getUpdateInfo/'+id),
	            /* type: BootstrapDialog.TYPE_SUCCESS, */
	            size: BootstrapDialog.SIZE_SMALL, 
	            buttons: [{
	                label: '取消',
	                cssClass : "btn btn-info",
	                action: function (dialog) {
	                	dialog.close();
	                }
	            }, {
	                label: '确定',
	                cssClass : "btn btn-info",
	                action: function (dialog) {
	                	
	                	
	    				$.ajax({
	    					url:"/menuController/updateInfo",
							data:{text:$("#text").val(),url:$("#url").val(),button:$("#button").val(),perms:$("#perms").val(),id:id},
	    					dataType:"json",
	    					type:"post",
	    					async:true,
	    					success:function(result){
	    						if(result){
	        						dialog.close();
	        						toastr.success("修改成功");
	        						$.fn.zTree.init($("#treeDemo"), setting);
	    						}else{
	    							toastr.error("修改失败")
	    						}

	    					},
	    					error:function(){
	    						toastr.error("系统异常,请联系管理员");
	    					}
	    				})
	                }
	            }]
	        });
		}
		
		function deleteNode(id){
			$.ajax({
				url:"/menuController/deleteInfo/"+id,
				data:{},
				type:"post",
				async:false,//同步
				dataType:"json",
				success:function(result){
					if(result){
						toastr.success("删除成功");
						$.fn.zTree.init($("#treeDemo"), setting);	
					}else{
						toastr.error("删除失败")
					}
				},
				error:function(){
					msg("系统错误，请联系管理员",3000);
				}
			})
		} 
</script>
</body>
</html>